﻿using Feicui.BeautyCRM.Factory;
using Feicui.BeautyCRM.IDAL;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using System.Text;
using System.Threading.Tasks;

namespace Feicui.BeautyCRM.BLL
{
    /// <summary>
    /// 增删改查业务层基类
    /// </summary>
    /// <typeparam name="T"></typeparam>
    public abstract class BaseService<T> where T : class,new()
    {
        /// <summary>
        /// 获取DbSession的实例（拥有所有操作类的实例）
        /// </summary>
        public IDbSession Db
        {
            get { return DbSessionFactory.GetCurrentDbSession(); }
        }

        public IBaseDal<T> CurrentDal { get; set; }

        public BaseService() 
        {
            GetCurrentDal();
        }
        /// <summary>
        /// 子类继承的时候让子类将IBaseDal<T>所需要的类型传进来
        /// </summary>
        public abstract void GetCurrentDal();

        #region  查询 
        /// <summary>
        /// 查询 
        /// </summary>
        /// <param name="wherelambda"></param>
        /// <returns></returns>
        public IQueryable<T> QueryEntitys(Expression<Func<T, bool>> wherelambda)
        {
            return CurrentDal.QueryEntitys(wherelambda);
        }
        /// <summary>
        /// 分页
        /// </summary>
        /// <typeparam name="S"></typeparam>
        /// <param name="pageSize"></param>
        /// <param name="pageIndex"></param>
        /// <param name="totle"></param>
        /// <param name="wherelambda"></param>
        /// <param name="ordlambda"></param>
        /// <param name="isAsc"></param>
        /// <returns></returns>
        public IQueryable<T> QueryPageOrdEntitys<S>(int pageSize, int pageIndex, out int totle, Expression<Func<T, bool>> wherelambda, Expression<Func<T, S>> ordlambda, bool isAsc = true)
        {
            return CurrentDal.QueryPageEntitys(pageSize, pageIndex, out totle, wherelambda, ordlambda, isAsc);
        }

        #endregion

        #region 添加

        /// <summary>
        /// 添加
        /// </summary>
        /// <param name="entity"></param>
        /// <returns></returns>
        public T AddEntity(T entity)
        {
            CurrentDal.AddEntity(entity);
            Db.SaveChanges();
            return entity;
        }
        #endregion

        #region  修改

        /// <summary>
        /// 修改
        /// </summary>
        /// <param name="entity"></param>
        /// <returns></returns>
        public bool EditEntity(T entity)
        {
            CurrentDal.EditEntity(entity);
            return Db.SaveChanges() > 0;
        }
        #endregion

        #region  删除

        /// <summary>
        /// 删除
        /// </summary>
        /// <param name="entity"></param>
        /// <returns></returns>
        public bool DelEntity(T entity)
        {
            CurrentDal.DelEntity(entity);
            return Db.SaveChanges() > 0;
        }
        #endregion

        #region  提交

        /// <summary>
        /// 提交
        /// </summary>
        /// <returns></returns>
        public int SaveChanges()
        {
            return Db.SaveChanges();
        }
        #endregion
    }
}
